/**
 * 功能描述 导航栏
 */

define(function (require, exports, module) {
	var Subnav = require("subnav");
	var aw = require("ajaxwrapper")
	var Dialog = require("dialog");
	var Form = require("form");
	var store =require("store");
	var activeUser = store.get("user");	
	var enmu = require("enums");
	var register = require("../store/register");
	
	var years=[];
	for(var i=0;i<=6;i++){
		var obj={};
		var year = moment().format('YYYY')-3;
		obj.key=year*1+i;
		obj.value=year*1+i;		
		years.push(obj);
	}
		
    var instance = {
        initComponent: function (params, widget) {
        	var subnav=new Subnav({
        		parentNode:".J-subnav",
				model : {
					title:"邮寄登记",
					items : [{
						id : "search",
						type : "search",
						show: false,
						placeholder:"姓名/手机/身份证",
						handler : function(str){
							var grid = widget.get("grid");
                    		var obj={
	                   				s:str,		                   			
	                   				"plan.pkPlan":widget.get("pkPlan"),
	                   				properties:"member.personalInfo.name,member.personalInfo.idNumber,member.personalInfo.mobilePhone",
        	     					fetchProperties:register.fetchProperties
                    		}	                    		
							aw.ajax({
								url:"api/mail/searchmail",
								data:obj,
								dataType:"json",
								success:function(data){
									grid.setData(data);
								}
							});
						  
						}
					},{
						id:"plan",
						tip: "计划",
						type:"button",
						show: false
					},{
			            id: "product",
			            tip: "产品",
			            type: "buttongroup",
			            all:{
			            	first:true,
			            	show:false,
			            },
			            keyField:"pkProduct",
			            valueField:"name",
			            handler: function(key) {
			            	widget.get("grid").refresh();
			            }
			        },{
			            id: "status",
			            tip: "状态",
			            type: "buttongroup",
			            show: false,
			            items: [{
			                key: "false",
			                value: "未邮寄"
			            }, {
			                key: "true",
			                value: "已邮寄"
			            }, {
			                key: "",
			                value: "全部"
			            }],
			            handler: function(key) {
			            	widget.get("grid").refresh();
			            }
			        },{
						id : "operation",
						type : "button",
						text:"批量操作",
						show: false,
						handler:function(){
							var form = new Form({
								parentNode:".J-form-dialog",
								autoRender:false,
								model:{
									id:"importForm",
									items:[{
									    	name:"pkPlan",
									    	type:"hidden",
									    	defaultValue:widget.get("pkPlan")
									    },{
											name:"address",
											type:"place",
											label:"区域",
											style:{
												value:"width:75%;"
											},
										},{
											name:"product",
											type:"select",
											url:"api/product/queryproductbymail",
											params:{
												fetchProperties:"pkProduct,name"
											},
											keyField:"pkProduct",
											valueField:"name",
											label:"产品名称",
											validate : [ "required" ],
											style:{
												value:"width:75%;"
											},
										},{
											name:"mailMethod",
											type:"select",
											options:enmu["com.eling.elcms.travel.model.Mail.MailMethod"],
											label:"邮寄方式",	
											validate : [ "required" ],
											style:{
												value:"width:75%;"
											},
										}],
									defaultButton:false
								}
							});
							Dialog.showComponent(form,{
								title:"批量操作",
								events : {				
								},
								setStyle:function(){
									$(".el-dialog-modal .modal").css({
										"top":"10%",
										"width":"40%",
										 "left" : "30%",
									});
								},
								confirm:function(){
									var data=form.getData();
									if(form.valid()){
										aw.ajax({
											url:"api/mail/savemail",
											data:data,
											dataType:"json",
											success:function(data){
												widget.get("grid").refresh();
											}
										});			
									}else{
										return false;
									}
								}			
							});
						}
					},{
						id : "toexcel",
						type : "button",
						text:"导出",
						show: false,
						handler:function(){ 
        					var gridData = widget.get("grid").getData();
        					if(gridData.length == 0){
        						Dialog.alert({
        							content : "没有数据，无法导出！"
        						});
        						return false;
        					}else{
        						var su = widget.get("subnav");
        						var s=su.getValue("search");
        						var url="api/mail/toexcel"+"?status="+su.getValue("status")+
        						"&plan="+su.getValue("plan")+"&productPk="+su.getValue("product");       						
        						
        							//url=url+"&s="+s;
        						
        						window.open(url);
    							return false;
        					}
	 					}
					},{
						id:"year",
						type:"buttongroup",
						items:years,						
						handler:function(key,element){						
							widget.get("indexgrid").refresh();
					   }					
					},{
						id : "return",
						type : "button",
						text:"返回",
						show: false,
						handler:function(){
							 widget.hide([".J-grid-register"]).show([".J-grid"]);
							 widget.get("subnav").show(["year"]).hide(["search","product","status","operation","toexcel","return"]);	
							 widget.get("indexgrid").refresh();
						}
					}]
				}
        	});
        	widget.set("subnav",subnav);
        }
    };
    module.exports = instance;
});
